package com.techysky.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.techysky.entity.AchievementDistribution;
import com.techysky.entity.ExamRecord;
import com.techysky.entity.Paper;
import com.techysky.entity.UserGroupStatistics;
import com.techysky.entity.Usergroups;
import com.techysky.repository.ExamRecordRepository;
import com.techysky.repository.PaperRepository;
import com.techysky.repository.UsergroupsRepository;

@Service
public class ExamRecordService {
	@Resource
	private ExamRecordRepository examRecordRepository;
	@Resource
	private PaperManagerService paperManagerService;
	@Autowired
	private  UsergroupsRepository usergroupsRepository;
	@Resource
	private PaperRepository paperRepository;
	
	public AchievementDistribution achievementDistribution(String paperId) {
		Paper paper=paperManagerService.findById(paperId);
		Set<ExamRecord> examRecordSet=paper.getExamRecord();
		AchievementDistribution ad=new AchievementDistribution();
		List<Usergroups> usergroupsList=usergroupsRepository.findAll();//查询所有的用户组
		
		double pscore;
		List<UserGroupStatistics> userGroupStatisticsList=new ArrayList<>();
		for (Usergroups usergroups : usergroupsList) {
			String[] users=usergroups.getUser().split(",");//每组中所含有的所有成员
			int gtotal=users.length;//组中含有的总人数
			double stotal=0;//总分数
			for (ExamRecord examRecord : examRecordSet) {
				if(Arrays.asList(users).contains(examRecord.getUserId())){
					stotal=stotal+examRecord.getScore();
				}
			}
			DecimalFormat df = new DecimalFormat("#0.0");    
			pscore=stotal/gtotal;
			UserGroupStatistics userGroupStatistics=new UserGroupStatistics();
			userGroupStatistics.setName(usergroups.getName());
			userGroupStatistics.setPscore(df.format(pscore));
			userGroupStatisticsList.add(userGroupStatistics);
		}
		for (ExamRecord examRecord : examRecordSet) {
			if((examRecord.getScore()/examRecord.getTotalScore())<0.5){
				int grade1=ad.getGrade1();
				ad.setGrade1(++grade1);
			}else if((examRecord.getScore()/examRecord.getTotalScore())<0.6){
				int grade2=ad.getGrade2();
				ad.setGrade2(++grade2);
			}else if((examRecord.getScore()/examRecord.getTotalScore())<0.7){
				int grade3=ad.getGrade3();
				ad.setGrade3(++grade3);
			}else if((examRecord.getScore()/examRecord.getTotalScore())<0.85){
				int grade4=ad.getGrade4();
				ad.setGrade4(++grade4);
			}else if((examRecord.getScore()/examRecord.getTotalScore())<1){
				int grade5=ad.getGrade5();
				ad.setGrade5(++grade5);
			}
			
		}
		ad.setUserGroupStatisticsList(userGroupStatisticsList);
		return ad;
	}
	public List<Paper> achievementStatistics(PageRequest pageable, String search) {
		 Page<Paper> papers= paperRepository.findAll((Root<Paper> root, CriteriaQuery<?> query, CriteriaBuilder cb)-> {
			List<Predicate> predicates = new ArrayList<>();
			if (!StringUtils.isEmpty(search)) {
				predicates.add(cb.like(root.get("name").as(String.class), "%" + search + "%"));
			}
			predicates.add(cb.like(root.get("examType").as(String.class), "%" + "考试试卷" + "%"));
			query.orderBy(cb.desc(root.get("createDate")));
			Predicate[] pre = new Predicate[predicates.size()];
			return query.where(predicates.toArray(pre)).getRestriction();
		}
		, pageable);
		 List<Paper> paperList=papers.getContent();
		 for (Paper paper : paperList) {
				Set<ExamRecord> examRecordSet=paper.getExamRecord();
				int totalNum=examRecordSet.size();//参加考试的总人数
				double hg=0;//合格的总人数
				if(totalNum==0){
					paper.setQrate("0.0");
					paper.setTotalPeople(totalNum);
					continue;
				}
				for (ExamRecord examRecord : examRecordSet) {
					if((examRecord.getScore()/examRecord.getTotalScore())>=0.6){
					  ++hg;
					}
				}
				//合格率计算
				DecimalFormat df = new DecimalFormat("#0.0"); 
				
				paper.setQrate(df.format((hg/totalNum)*100));
				paper.setTotalPeople(totalNum);
				
			}
		 return paperList;
	}
}
